﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
namespace WpfApplication2
{
    /// <summary>
    /// 启动的时候加载一个音频，失去焦点暂停，焦点恢复重新开始播放, 关闭的是询问是关闭
    /// </summary>
    public partial class WindowEventTest : Window
    {
        public WindowEventTest()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window_Loaded);
            this.Deactivated += new EventHandler(Window_Deactivated);
            this.Activated += new EventHandler(Window_Activated);
            this.Closing += new CancelEventHandler(Window_Closing);

        }
        private bool isPlaying;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 窗体加载时，显示当前日期及开始播放媒体
            myMedia.Source = new Uri("c:\\project\\test.m4a",
           UriKind.Absolute);

            txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            myMedia.Play();
            isPlaying = true;
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            // 如果窗体被激活，则继承播放媒体
            if (!isPlaying)
            {
                myMedia.Play();
                isPlaying = true;
            }
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            // 如果窗体失去焦点，则暂停播放媒体
            if (isPlaying)
            {
                myMedia.Pause();
                isPlaying = false;
            }
        }


        private void Window_Closing(object sender,
      CancelEventArgs e)
        {
            // 点击窗体的“关闭”按钮，询问用户是否退出程序

            string message = "确定要退出吗？";
            MessageBoxButton button = MessageBoxButton.OKCancel;
            MessageBoxResult result = MessageBox.Show(
            message, "title", button, MessageBoxImage.Question);
            if (result == MessageBoxResult.Cancel)
            {
                e.Cancel = true; // 取消退出
            }
        }


        private void btnMessage_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
